package com.aplink.generic.widgets;

import java.text.ParseException;

import android.text.InputFilter;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.EditTextType;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.pojo.controlspec.textview.DisplayStyle;
import com.aplink.generic.util.DateTimeUtils;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.objectrule.ObjectBaseRule;

public class ApLinkEditText extends ApLinkBaseView {
    private static final long serialVersionUID = 1116835381148753997L;
    private transient final EditText mEditText;
    private final EditTextSpec mEditTextSpecFormat;
    private OnGetObjProInfo mGetObjProInfo;
    private EditTextType mInputType;

    public ApLinkEditText(final GenericFragment dataEntryActivity,
            final ControlSpec controlSpec, final View view) {
        super(dataEntryActivity, controlSpec, view);
        this.mEditText = (EditText) view;
        this.mEditTextSpecFormat = (EditTextSpec) controlSpec;
    }

    @Override
    public void clear() {
        this.mEditText.setText("");
    }

    @Override
    public GenericAttribute getAttribute() {
        final String name = this.mEditTextSpecFormat.getMappingName();
        final String dataObj = this.mEditTextSpecFormat.getDataObject();
        ObjPropInfo objPropInfo = null;
        if (name == null) {
            objPropInfo = this.mGetObjProInfo.getObjProInfoTemp();
        } else {
            objPropInfo = getGenericFragment().getContentData().get(
                    dataObj + "." + name);
        }

        String value = this.mEditText.getText().toString();
        final DisplayStyle displayType = this.mEditTextSpecFormat
                .getDisplayStyle();
        if (displayType.equals(DisplayStyle.DATETIME)
                || displayType.equals(DisplayStyle.CURRENT_DATETIME)) {
            try {
                final long miliseconds = DateTimeUtils.parse(value);
                value = String.valueOf(miliseconds / 1000L);
            } catch (final ParseException e) {
                LogUtil.error(e.getMessage(), "Get attribute from EditText",
                        getClass());
            }
        }
        return GenericAttribute.obtain(value, objPropInfo);
    }

    public EditText getEditText() {
        return this.mEditText;
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public EditText getView() {
        return this.mEditText;
    }

    @Override
    protected void initViewProperties() {
        final boolean isReadOnly = this.mEditTextSpecFormat.isReadOnly();
        if (isReadOnly) {
            this.mEditText.setKeyListener(null);
            this.mEditText.setEnabled(false);
            this.mEditText.setCursorVisible(false);
            this.mEditText.setFocusable(false);
            this.mEditText.setFocusableInTouchMode(false);
        }

        this.mInputType = this.mEditTextSpecFormat.getDataInputType();
        if (this.mInputType != null) {
            this.mEditText.setInputType(this.mInputType.getValue());
        }

        if (this.mInputType.equals(EditTextType.PASSWORD)) {
            this.mEditText.setTransformationMethod(PasswordTransformationMethod
                    .getInstance());
        }
        this.mEditText
                .setFilters(new InputFilter[] { new InputFilter.LengthFilter(
                        this.mEditTextSpecFormat.getMaxLength()) });

        if (this.mEditTextSpecFormat.hasBaseRule()) {
            final ObjectBaseRule baseRule = this.mEditTextSpecFormat
                    .getObjectBaseRule();
            final boolean isEnable = baseRule.isSatisfiedCondition(
                    getPassedGenericObject(), -1);
            this.mEditText.setEnabled(isEnable);
        }

        if (this.mEditTextSpecFormat.getGetObjProInfo() != null) {
            this.mGetObjProInfo = this.mEditTextSpecFormat.getGetObjProInfo();
        }
    }

    @Override
    protected void loadBindingObject() {
        final String key = String.format("%s.%s",
                this.mEditTextSpecFormat.getDataObject(),
                this.mEditTextSpecFormat.getMappingName());
        String content = null;
        if (this.mEditTextSpecFormat.getValue() != null) {
            content = this.mEditTextSpecFormat.getValue();
        } else {
            content = getHashPassedValues().get(key);
        }

        final DisplayStyle displayStyle = this.mEditTextSpecFormat
                .getDisplayStyle();
        try {
            if (displayStyle.equals(DisplayStyle.DATETIME)) {
                if (content != null) {
                    final long milliseconds = Long.parseLong(content);
                    content = DateTimeUtils.getDateTime(milliseconds);
                } else {
                    content = "";
                }
            }
        } catch (final ParseException e) {
            LogUtil.error(e.getMessage(), "Load passed value into view",
                    getClass());
        }
        if (getPassedContent() != null) {
            this.mEditText.setText(getPassedContent());
        } else {
            if (TextUtils.isEmpty(content)) {
                this.mEditText.getText().clear();
            } else {
                this.mEditText.setText(content);
            }
        }
    }

    @Override
    protected void loadDefaultObject() {
        if (this.mEditTextSpecFormat.getDisplayStyle().equals(
                DisplayStyle.CURRENT_DATETIME)) {
            try {
                final long milliseconds = DateTimeUtils.getCurrentDateTime();
                final String content = DateTimeUtils.getDateTime(milliseconds);
                this.mEditText.setText(content);
            } catch (final ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void setEventListener() {
        final boolean isSearch = this.mEditTextSpecFormat.isSearch();
        if ((isSearch == true)
                && (this.mEditTextSpecFormat.getSearchControl() != null)) {
            final int idSearchControl = ResourcesHelper
                    .getViewIdByName(this.mEditTextSpecFormat
                            .getSearchControl());
            if (idSearchControl == ResourcesHelper.RESOURCE_NOT_FOUND) {
                return;
            }
        }
    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        this.mGetObjProInfo = getObjProInfo;
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }
}
